{% load url from future %}
{% load admin_static i18n grp_tags %}

<!-- group -->
<div class="grp-group grp-stacked {% if inline_admin_formset.opts.classes %} {{ inline_admin_formset.opts.classes|join:" " }}{% endif %}"
    id="{{ inline_admin_formset.formset.prefix }}-group">
    <h2 class="grp-collapse-handler">{% if inline_admin_formset.opts.title %}{{ inline_admin_formset.opts.title }}{% else %}{{ inline_admin_formset.opts.verbose_name_plural|capfirst }}{% endif %}</h2>
    <ul class="grp-tools">
        <li><a href="javascript://" class="grp-icon grp-open-handler" title="{% trans 'Open All Items' %}"></a></li>
        <li><a href="javascript://" class="grp-icon grp-close-handler" title="{% trans 'Close All Items' %}"></a></li>
        <li><a href="javascript://" class="grp-icon grp-add-handler" title="{% trans 'Add Item' %}"> </a></li>
    </ul>
    {{ inline_admin_formset.formset.management_form }}
    {{ inline_admin_formset.formset.non_form_errors }}
    <!-- container -->
    <div class="grp-items">
        {% with inline_admin_formset.opts.sortable_field_name|default:"" as sortable_field_name %}
        {% for inline_admin_form in inline_admin_formset|formsetsort:sortable_field_name %}
            <!-- element -->
            <div class="grp-module {{ inline_admin_formset.opts.inline_classes|join:" "|default:"grp-collapse grp-closed" }}{% if inline_admin_form.original or inline_admin_form.show_url %} has_original{% endif %}{% if forloop.last %} grp-empty-form{% endif %}"
                id="{{ inline_admin_formset.formset.prefix }}{% if forloop.last %}-empty{% else %}{{ forloop.counter0 }}{% endif %}">
                <h3 class="grp-collapse-handler">{{ inline_admin_formset.opts.verbose_name }}&nbsp;&nbsp;{% if inline_admin_form.original %}{{ inline_admin_form.original }}{% endif %}</h3>
                <ul class="grp-tools">
                    {% if inline_admin_form.show_url %}<li><a href="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.id }}/" class="grp-icon grp-viewsite-link" title="{% trans 'View on Site' %}" target="_blank"></a></li>{% endif %}
                    {% if inline_admin_formset.opts.sortable_field_name %}
                        <li><a href="javascript://" class="grp-icon grp-drag-handler" title="{% trans 'Move Item' %}"></a></li>
                    {% endif %}
                    {% if inline_admin_form.original %}
                        {% if inline_admin_formset.formset.can_delete %}
                            <li class="grp-delete-handler-container">{{ inline_admin_form.deletion_field.field }}<a href="javascript://" class="grp-icon grp-delete-handler" title="{% trans 'Delete Item' %}"></a></li>
                        {% else %}
                            <li><span class="grp-icon">&nbsp;</span></li>
                        {% endif %}
                    {% else %}
                        <li><a href="javascript://" class="grp-icon grp-remove-handler" title="{% trans 'Delete Item' %}"></a></li>
                    {% endif %}
                </ul>
                {% if inline_admin_form.form.non_field_errors %}
                    {{ inline_admin_form.form.non_field_errors }}
                {% endif %}
                {% for fieldset in inline_admin_form %}
                    {% include "admin/includes/fieldset_inline.html" %}
                {% endfor %}
                {{ inline_admin_form.fk_field.field }}
                {% if inline_admin_form.has_auto_field or inline_admin_form.needs_explicit_pk_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
            </div>
        {% endfor %}
        {% endwith %}
        {{ inline_admin_formset.extra_forms }}
    </div>
    <div class="grp-module grp-transparent">
        <div class="grp-row">
            <a href="javascript://" class="grp-add-handler"><strong>{% blocktrans with inline_admin_formset.opts.verbose_name as verbose_name %}Add another {{ verbose_name }}{% endblocktrans %}</strong></a>
            <ul class="grp-tools">
                <li><a href="javascript://" class="grp-icon grp-add-handler" title="{% trans 'Add Item' %}"> </a></li>
            </ul>
        </div>
    </div>
</div>

<script type="text/javascript">
(function($) {
    $(document).ready(function() {
        var prefix = "{{ inline_admin_formset.formset.prefix }}";
        var related_lookup_fields_fk = {% get_related_lookup_fields_fk inline_admin_formset.opts %};
        var related_lookup_fields_m2m = {% get_related_lookup_fields_m2m inline_admin_formset.opts %};
        var related_lookup_fields_generic = {% get_related_lookup_fields_generic inline_admin_formset.opts %};
        var autocomplete_fields_fk = {% get_autocomplete_lookup_fields_fk inline_admin_formset.opts %};
        var autocomplete_fields_m2m = {% get_autocomplete_lookup_fields_m2m inline_admin_formset.opts %};
        var autocomplete_fields_generic = {% get_autocomplete_lookup_fields_generic inline_admin_formset.opts %};
        $.each(related_lookup_fields_fk, function() {
            $("#{{ inline_admin_formset.formset.prefix }}-group > div.grp-items > div:not(.grp-empty-form)")
            .find("input[name^='" + prefix + "'][name$='" + this + "']")
            .grp_related_fk({lookup_url:"{% url 'grp_related_lookup' %}"});
        });
        $.each(related_lookup_fields_m2m, function() {
            $("#{{ inline_admin_formset.formset.prefix }}-group > div.grp-items > div:not(.grp-empty-form)")
            .find("input[name^='" + prefix + "'][name$='" + this + "']")
            .grp_related_m2m({lookup_url:"{% url 'grp_m2m_lookup' %}"});
        });
        $.each(related_lookup_fields_generic, function() {
            var content_type = this[0],
                object_id = this[1];
            $("#{{ inline_admin_formset.formset.prefix }}-group > div.grp-items > div:not(.grp-empty-form)")
            .find("input[name^='" + prefix + "'][name$='" + this[1] + "']")
            .each(function() {
                var i = $(this).attr("id").match(/-\d+-/);
                if (i) {
                    var ct_id = "#id_" + prefix + i[0] + content_type,
                        obj_id = "#id_" + prefix + i[0] + object_id;
                    $(this).grp_related_generic({content_type:ct_id, object_id:obj_id, lookup_url:"{% url 'grp_related_lookup' %}"});
                }
            });
        });
        $.each(autocomplete_fields_fk, function() {
            $("#{{ inline_admin_formset.formset.prefix }}-group > div.grp-items > div:not(.grp-empty-form)")
            .find("input[name^='" + prefix + "'][name$='" + this + "']")
            .each(function() {
                $(this).grp_autocomplete_fk({
                    lookup_url:"{% url 'grp_related_lookup' %}",
                    autocomplete_lookup_url:"{% url 'grp_autocomplete_lookup' %}"
                });
            });
        });
        $.each(autocomplete_fields_m2m, function() {
            $("#{{ inline_admin_formset.formset.prefix }}-group > div.grp-items > div:not(.grp-empty-form)")
            .find("input[name^='" + prefix + "'][name$='" + this + "']")
            .each(function() {
                $(this).grp_autocomplete_m2m({
                    lookup_url:"{% url 'grp_m2m_lookup' %}",
                    autocomplete_lookup_url:"{% url 'grp_autocomplete_lookup' %}"
                });
            });
        });
        $.each(autocomplete_fields_generic, function() {
            var content_type = this[0],
                object_id = this[1];
            $("#{{ inline_admin_formset.formset.prefix }}-group > div.grp-items > div:not(.grp-empty-form)")
            .find("input[name^='" + prefix + "'][name$='" + this[1] + "']")
            .each(function() {
                var i = $(this).attr("id").match(/-\d+-/);
                if (i) {
                    var ct_id = "#id_" + prefix + i[0] + content_type,
                        obj_id = "#id_" + prefix + i[0] + object_id;
                    $(this).grp_autocomplete_generic({
                        content_type:ct_id,
                        object_id:obj_id,
                        lookup_url:"{% url 'grp_related_lookup' %}",
                        autocomplete_lookup_url:"{% url 'grp_autocomplete_lookup' %}"
                    });
                }
            });
        });
        
        $("#{{ inline_admin_formset.formset.prefix }}-group").grp_inline({
            prefix: "{{ inline_admin_formset.formset.prefix }}",
            onAfterRemoved: function(inline) {},
            onAfterAdded: function(form) {
                grappelli.reinitDateTimeFields(form);
                grappelli.updateSelectFilter(form);
                $.each(related_lookup_fields_fk, function() {
                    form.find("input[name^='" + prefix + "'][name$='" + this + "']")
                    .grp_related_fk({lookup_url:"{% url 'grp_related_lookup' %}"});
                });
                $.each(related_lookup_fields_m2m, function() {
                    form.find("input[name^='" + prefix + "'][name$='" + this + "']")
                    .grp_related_m2m({lookup_url:"{% url 'grp_m2m_lookup' %}"});
                });
                $.each(related_lookup_fields_generic, function() {
                    var content_type = this[0],
                        object_id = this[1];
                    form.find("input[name^='" + prefix + "'][name$='" + this[1] + "']")
                    .each(function() {
                        var i = $(this).attr("id").match(/-\d+-/);
                        if (i) {
                            var ct_id = "#id_" + prefix + i[0] + content_type,
                                obj_id = "#id_" + prefix + i[0] + object_id;
                            $(this).grp_related_generic({content_type:ct_id, object_id:obj_id, lookup_url:"{% url 'grp_related_lookup' %}"});
                        }
                    });
                });
                $.each(autocomplete_fields_fk, function() {
                    form.find("input[name^='" + prefix + "'][name$='" + this + "']")
                    .each(function() {
                        $(this).grp_autocomplete_fk({
                            lookup_url:"{% url 'grp_related_lookup' %}",
                            autocomplete_lookup_url:"{% url 'grp_autocomplete_lookup' %}"
                        });
                    });
                });
                $.each(autocomplete_fields_m2m, function() {
                    form.find("input[name^='" + prefix + "'][name$='" + this + "']")
                    .each(function() {
                        $(this).grp_autocomplete_m2m({
                            lookup_url:"{% url 'grp_m2m_lookup' %}",
                            autocomplete_lookup_url:"{% url 'grp_autocomplete_lookup' %}"
                        });
                    });
                });
                $.each(autocomplete_fields_generic, function() {
                    var content_type = this[0],
                        object_id = this[1];
                    form.find("input[name^='" + prefix + "'][name$='" + this[1] + "']")
                    .each(function() {
                        var i = $(this).attr("id").match(/-\d+-/);
                        if (i) {
                            var ct_id = "#id_" + prefix + i[0] + content_type,
                                obj_id = "#id_" + prefix + i[0] + object_id;
                            $(this).grp_autocomplete_generic({
                                content_type:ct_id,
                                object_id:obj_id,
                                lookup_url:"{% url 'grp_related_lookup' %}",
                                autocomplete_lookup_url:"{% url 'grp_autocomplete_lookup' %}"
                            });
                        }
                    });
                });
                form.grp_collapsible();
                form.find(".grp-collapse").grp_collapsible();
            }
        });
        
        {% if inline_admin_formset.opts.sortable_field_name %}
        $("#{{ inline_admin_formset.formset.prefix }}-group > div.grp-items").sortable({
            handle: "a.grp-drag-handler",
            items: "div.grp-dynamic-form",
            axis: "y",
            appendTo: 'body',
            forceHelperSize: true,
            placeholder: 'grp-module ui-sortable-placeholder',
            forcePlaceholderSize: true,
            containment: '#{{ inline_admin_formset.formset.prefix }}-group > div.grp-items',
            tolerance: 'pointer',
            start: function(evt, ui) {
                ui.placeholder.height(ui.item.height() + 12);
            }
        });
        $("#{{ opts.module_name }}_form").bind("submit", function(){
            var sortable_field_name = "{{ inline_admin_formset.opts.sortable_field_name }}",
                sortable_excludes = {% get_sortable_excludes inline_admin_formset.opts %},
                i = 0;
            $("#{{ inline_admin_formset.formset.prefix }}-group").find("div.grp-dynamic-form").each(function(){
                var fields = $(this).find("fieldset :input[value][value!=''][type!=radio][type!=checkbox],input:checked,input[type=file]"),
                    submit_values = false,
                    has_value;
                $(fields).each(function() {
                    // position is being updated if
                    // a) the field has a value
                    // b) if the field is not exluded with sortable_excludes (e.g. with default values)
                    has_value = $(this).val() || ($(this).attr('type') == 'file' && $(this).siblings('a').length);
                    if (has_value && $(this).attr("name") && $.inArray($(this).attr("name").split("-").pop(), sortable_excludes) == -1) {
                        submit_values = true;
                    }
                });
                if (submit_values) {
                    $(this).find("input[name$='"+sortable_field_name+"']").val(i);
                    i++;
                }
            });
        });
        {% endif %}
        
    });
})(grp.jQuery);
</script>
